package com.sskj.gcc.util;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.gcc.R;

public class PhoneNumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private String[] countryNum;
    private String[] countryName;
    public PhoneNumAdapter(Context context){
        this.context = context;
        countryNum = context.getResources().getStringArray(R.array.app_country_num);
        countryName = context.getResources().getStringArray(R.array.app_country_name);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_phone_num,parent,false);
        return new PhoneNumViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof PhoneNumViewHolder){
            ((PhoneNumViewHolder) holder).nameTv.setText(countryName[position]);
            ((PhoneNumViewHolder) holder).numTv.setText(countryNum[position]);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(phoneNumSelectListener!=null){
                        phoneNumSelectListener.onSelectNum(countryName[position],countryNum[position]);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return countryNum.length;
    }

    private class PhoneNumViewHolder extends RecyclerView.ViewHolder{

        private ImageView imgIv;
        private TextView nameTv;
        private TextView numTv;
        public PhoneNumViewHolder(View itemView) {
            super(itemView);
//            imgIv = itemView.findViewById(R.id.iv_item_phone_num_img);
            nameTv = itemView.findViewById(R.id.tv_item_phone_num_name);
            numTv = itemView.findViewById(R.id.tv_item_phone_num_num);
        }
    }

    private PhoneNumSelectListener phoneNumSelectListener;

    public void setPhoneNumSelectListener(PhoneNumSelectListener phoneNumSelectListener) {
        this.phoneNumSelectListener = phoneNumSelectListener;
    }
}
